//系统配置-操作分组-表单-数据源
App.util.ns('Admin.Access.OperationGroup.Form');
Admin.Access.OperationGroup.Form.DataSource = function(config){
    this.config = config;
    this.name = this.config.name;
    this.form = null;
    this.frame = null;
    this.inputFrame = null;
    this.viewFrame = null;
    //初始化
    this.init();
};
Admin.Access.OperationGroup.Form.DataSource.prototype = {
    constructor: Admin.Access.OperationGroup.Form.DataSource,
    init: function(){
        this.frame = $([
            '<div class="input-container">',
            '   <div class="input-group">',
            '       <input type="text" class="form-control" name="data_source_source" placeholder="class@method:params">',
            '       <span class="input-group-addon" style="border-left: none;border-right: none;">-</span>',
            '       <input type="text" class="form-control" name="data_source_key" placeholder="key">',
            '       <span class="input-group-addon" style="border-left: none;border-right: none;">-</span>',
            '       <input type="text" class="form-control" name="data_source_name" placeholder="name">',
            '   </div>',
            '   <span class="form-static" style="display: none;"></span>',
            '</div>'
        ].join(''));
        this.inputFrame = this.frame.find('.input-group');
        this.viewFrame = this.frame.find('.form-static');
        return this;
    },
    setForm: function(form){
        this.form = form;
    },
    change: function (val) {//改变
        var dataSource = JSON.parse(val) || {};
        this.frame.find('input[name="data_source_source"]').val(dataSource.source || '');
        this.frame.find('input[name="data_source_key"]').val(dataSource.key || '');
        this.frame.find('input[name="data_source_name"]').val(dataSource.name || '');
        return this;
    },
    view: function () {//查看
        this.inputFrame.hide();
        this.viewFrame.show();
        return this;
    },
    flushView: function () {
        var value = JSON.parse(this.value());
        this.viewFrame.text(value.source + ' -- ' + value.key + ' -- ' + value.name).show();
    },
    reset: function () {
        this.viewFrame.hide();
        this.frame.find('input[name="data_source_source"]').val('');
        this.frame.find('input[name="data_source_key"]').val('');
        this.frame.find('input[name="data_source_name"]').val('');
        this.frame.show();
        return this;
    },
    edit: function () {
        this.viewFrame.hide();
        this.inputFrame.show();
        return this;
    },
    value: function () {
        var self = this;
        return JSON.stringify({
            source: self.frame.find('input[name="data_source_source"]').val(),
            key: self.frame.find('input[name="data_source_key"]').val(),
            name: self.frame.find('input[name="data_source_name"]').val()
        });
    },
    enable: function () {
        this.frame.find('input').prop('disabled', false);
        return this;
    },
    disable: function () {
        this.frame.find('input').prop('disabled', true);
        return this;
    },
    validate: function () {
        if (this.form.formItems.type.value() + '' !== '2') {
            return true;
        }
        var rs = true;
        this.frame.find('input').each(function () {
            if ($(this).val().length < 1) {
                $(this).addClass('error').one('change', function () {
                    $(this).removeClass('error');
                });
                rs = false;
            }
        });
        return rs;
    },
    destroy: function(){
        this.frame.remove();
    }
};
//添加到库
App.Form.lib['admin-access-operationGroup-dataSource'] = Admin.Access.OperationGroup.Form.DataSource;